//
//  WritePostViewController.m
//  cityStar
//
//  Created by joyban on 15-3-8.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "WritePostViewController.h"
#import "RJPlaceHolderTextView.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"
#import "UIActionSheet+Blocks.h"
#import "UpLoadImageManager.h"
#import "UIColor+HexColors.h"
#import "UserCityModel.h"

@interface WritePostViewController ()<UIPickerViewDelegate,UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet RJPlaceHolderTextView *contentText;
@property (weak, nonatomic) IBOutlet UIImageView *contentImage;
@property (strong, nonatomic)  NSString *Imagestring;
@property (nonatomic, strong) NSMutableArray *arrayRequest;
@property (weak, nonatomic) IBOutlet UITextField *selectField;
@property (weak, nonatomic) IBOutlet UITextField *titleFiel;
@property (strong, nonatomic)  NSString *cityid;

@end

@implementation WritePostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"社区论坛";
    [self addBackButton];
    self.Imagestring = @"";
    self.contentText.placeholder = @"请输入内容";
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithTitle:@"发送" actionBlock:^{
        NSDictionary *parameters = @{@"title":weakSelf.titleFiel.text,
                                     @"content":weakSelf.contentText.text,
                                     @"community":weakSelf.cityid,
                                     @"community_name":weakSelf.selectField.text,
                                     @"image[0]":weakSelf.Imagestring,
                                     };
        [HttpRequestManager postMethod:KinterFace_Releascposts parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            DLog(@"%@",response);
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"发布成功"];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }];
    self.arrayRequest = [[NSMutableArray alloc]init];
    UIPickerView *typePickerView = [[UIPickerView alloc]init];
    typePickerView.delegate = self;
    self.selectField.inputView = typePickerView;
    
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneButton,nil];
    [topView setItems:buttonsArray];
    [self.selectField setInputAccessoryView:topView];
    [self.titleFiel setInputAccessoryView:topView];
    [self.contentText setInputAccessoryView:topView];
    [self webRequest_GetMyUserCity];
}

-(void)viewWillAppear:(BOOL)animated
{
    
    self.contentText.placeholder = @"输入内容";
    self.contentText.placeholderColor = [UIColor colorWithWhite:0.797 alpha:1.000];
//    self.contentText.placeholder = @"请输入内容";
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(void)dismissKeyBoard
{
    if([self.selectField.text isEqualToString:@""])
    {
        UserCityModel *model = [UserCityModel objectFromDictionary:self.arrayRequest[0]];
        self.selectField.text = model.name;
        self.cityid = self.arrayRequest[0][@"id"];
    }
    [self.selectField resignFirstResponder];
    [self.titleFiel resignFirstResponder];
    [self.contentText resignFirstResponder];
}
- (void)webRequest_GetMyUserCity
{
    [HttpRequestManager getMethod:KinterFace_MyUserCity parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        NSLog(@"%@",response);
        if (success) {
            [self.arrayRequest addObjectsFromArray:response[@"value"][@"content"]];
        }
    }];
}


#pragma mark - pickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.arrayRequest.count;
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    UserCityModel *model = [UserCityModel objectFromDictionary:self.arrayRequest[row]];
    return model.name;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    UserCityModel *model = [UserCityModel objectFromDictionary:self.arrayRequest[row]];
    self.selectField.text = model.name;
    self.cityid = self.arrayRequest[row][@"id"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)photo_Action:(UIButton *)sender {
    __block UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    
    RIButtonItem *takePhotoButton = [RIButtonItem itemWithLabel:@"拍照" action:^{
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }];
    RIButtonItem *choosePhotoButton = [RIButtonItem itemWithLabel:@"从相册选取" action:^{
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }];
    
    UIActionSheet *imageSourcePickActionSheet;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imageSourcePickActionSheet = [[UIActionSheet alloc] initWithTitle:@"添加照片" cancelButtonItem:nil destructiveButtonItem:[RIButtonItem itemWithLabel:@"取消"] otherButtonItems:takePhotoButton,choosePhotoButton,nil];
    }else{
        imageSourcePickActionSheet = [[UIActionSheet alloc] initWithTitle:@"添加照片" cancelButtonItem:nil destructiveButtonItem:[RIButtonItem itemWithLabel:@"取消"] otherButtonItems:choosePhotoButton,nil];
    }
    [imageSourcePickActionSheet showInView:self.view];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissViewControllerAnimated:YES completion:^{
        [self.contentImage setImage:editingInfo[UIImagePickerControllerOriginalImage]];
        [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
            if (success) {
                DLog(@"%@",urlString);
                self.Imagestring = urlString;
            }
        }];
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end

